package com.phantom.reflection.clazz;

import com.phantom.Car;

import java.lang.reflect.Field;

public class Class01 {
    public static void main(String[] args) throws Exception {

        String classAllPath = "com.phantom.Car";
        Class<?> cls = Class.forName(classAllPath);

        System.out.println(cls); // class com.phantom.Car
        System.out.println(cls.getClass()); // 输出运行类型class java.lang.Class

        System.out.println(cls.getPackage().getName()); // com.phantom
        System.out.println(cls.getName()); // 全类名com.phantom.Car
        System.out.println(cls.getName());

        Car car = (Car) cls.newInstance(); // 通过cls创建对象实例
        System.out.println(car); // car.toString()

        Field brand = cls.getField("brand");
        System.out.println(brand.get(car)); // 宝马

        // 通过反射给属性赋值
        brand.set(car, "兰博基尼");
        System.out.println(brand.get(car)); // 兰博基尼

        Field[] fields = cls.getFields();
        for (Field field : fields) {
            System.out.println(field.get(car));
        }

    }
}
